package com.mytaobao.main.firsttab.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.util.UIUtils;
import com.mytaobao.R;


public class ButtomBtn extends LinearLayout {
    @Override
    public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
        super.setTouchscreenBlocksFocus(touchscreenBlocksFocus);
    }

    private ImageView iv;
    private TextView tv;

    boolean mSelected;
    String selectedTextColor = "#FF5000";
    String unSelectedTextColor = "#333333";

    int selectedIcon;
    int unSelectedIcon;

    String text;
    String className;

    public ButtomBtn(Context context) {
        super(context);
        init(context);
    }

    public ButtomBtn(Context context, AttributeSet attrs) {
        super(context, attrs);

        //读取xml配置到内存中
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ButtomBtn);
        if(null != a) {
            mSelected = a.getBoolean(R.styleable.ButtomBtn_ifSelected, false);
            text = a.getString(R.styleable.ButtomBtn_text);
            selectedIcon = a.getResourceId(R.styleable.ButtomBtn_selectedIcon, R.drawable.home_selected);
            unSelectedIcon = a.getResourceId(R.styleable.ButtomBtn_unSelectedIcon, R.drawable.home);
            className = a.getString(R.styleable.ButtomBtn_className);
            a.recycle();
        }
        init(context);
    }

    public ButtomBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context) {
        setOrientation(VERTICAL);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(layoutParams);
        setGravity(Gravity.CENTER);
        setPadding(UIUtils.dip2px(5),UIUtils.dip2px(5),UIUtils.dip2px(5), UIUtils.dip2px(5));

        View bottomBtnView = LayoutInflater.from(context).inflate(R.layout.bottom_btn_view, this, true);
        iv = (ImageView) bottomBtnView.findViewById(R.id.iv);
        tv = (TextView) bottomBtnView.findViewById(R.id.tv);
        tv.setText(text);
        if(mSelected) {
            tv.setTextColor(Color.parseColor(selectedTextColor));
            iv.setImageDrawable(getResources().getDrawable(selectedIcon));
        } else {
            tv.setTextColor(Color.parseColor(unSelectedTextColor));
            iv.setImageDrawable(getResources().getDrawable(unSelectedIcon));
        }
    }

    public void setTvColor(int color){
        tv.setTextColor(color);
    }

    /**
     * 切换是否选中，也就是切换icon和textview的color
     */
    public void toggleToSelected() {
            tv.setTextColor(Color.parseColor(selectedTextColor));
            iv.setImageDrawable(getResources().getDrawable(selectedIcon));
    }

    public void toggleToUnSelected() {
        tv.setTextColor(Color.parseColor(unSelectedTextColor));
        iv.setImageDrawable(getResources().getDrawable(unSelectedIcon));
    }

    public String getClassName() {
        return className;
    }

}

